/***************************************************************************
 *   Copyright (C) 2007 - 2009 by Andreas Theofilu                         *
 *   andreas@theosys.at                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation version 3 of the License.                *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef _IMPORT_H
#define _IMPORT_H

#include <qfile.h>
#include <qstring.h>
#include <QXmlReader>

#include "garmin.h"
#include "disassemble.h"

#define eMax 7

#define FLD_FIRST	1
#define FLD_LAST	30
#define CON_FIRST	100
#define CON_LAST	121
#define ATT_FIRST	200
#define ATT_LAST	200
#define ERR_FIRST	500
#define ERR_LAST	507

#define ERR_OK			500
#define ERR_NOFILE		501
#define ERR_TAGS		502
#define ERR_ALLOCGMN		503
#define ERR_ALLOCLAP		504
#define ERR_ALLOCPOINT		505
#define ERR_ALLOCRUN		506
#define ERR_ALLOCLIST		507

typedef struct
{
	int id;
	QString name;
} KEYS;

extern KEYS keys[];

class gmn_import : public QXmlDefaultHandler
{
	public:
	   gmn_import () { Initialize(); };
	   gmn_import (const QFile &qfile);
	   ~gmn_import () { if (qfstat) file.close(); };

	   void Initialize ();
	   void setFile (const QFile &qfile);
	   void setFile (const QString &sfile);
	   void setDisassemble (disassemble *dis) { ds = dis; };

	   int import ();
	   garmin_data *getGarminData() { if (gmn) return gmn; else return 0; };

	   QString getError (int err);
	   QString getError ();

	   bool startDocument ();
	   bool startElement (const QString&, const QString&, const QString& , const QXmlAttributes&);
	   bool endElement (const QString&, const QString&, const QString&);
	   bool characters (const QString&);

	protected:
	   unsigned int garmin_time (const QString&);
	   QString getKey (int pos);

	private:
	   disassemble *ds;
	   QFile file;
	   bool qfstat, qfopen;
	   int __error;
	   int indent, tpos, lpos, oldLPos;
	   int tk, first_tpos;
	   bool history, fakeLap;
	   QString con, subCon;
	   garmin_data *gmn, *gmn_lap, *gmn_track;
	   garmin_list *list, *list_lap, *list_track;
	   D1015 lap, *plap;
	   D1009 run, *prun;
	   D304 point;

};

#endif